//
//  YouHuiVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-13.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "YouHuiVC.h"
#import "YouHui.h"
#import "FindCell.h"

#define BASE_TOTAL_URL @"http://www.shihuo.cn/app3/getCouponsList?platform=ios&timestamp=1460375274&v=4.0.2&token=e1d003d5641b426a6515e791d9edb074&page=1&pageSize=30"
#define BASE_YOUHUI_URL @"http://www.shihuo.cn/app3/getCouponsList?platform=ios&timestamp=1460375358&v=4.0.2&token=0b67df5152f6b8b0e2083d1f9cb7a0b2&page=1&pageSize=30&type=0"
#define BASE_LIPIN_URL @"http://www.shihuo.cn/app3/getCouponsList?platform=ios&timestamp=1460375400&v=4.0.2&token=20090fb72bb66de887d2d8ebfeb2e27a&page=1&pageSize=30&type=1"

@interface YouHuiVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *moveLine;

@property (nonatomic, strong) NSArray *urlArr;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation YouHuiVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   // self.navigationController.navigationBarHidden = YES;
    self.title = @"优惠券";
    _dataArr = [[NSMutableArray alloc] init];
    _urlArr = @[BASE_TOTAL_URL,BASE_YOUHUI_URL,BASE_LIPIN_URL];
    [self setTitleView];
    [self setTopView];
    [self getDataWithURL:_urlArr[0]];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(_moveLine.mas_bottom).offset(0);
    }];
}
#pragma mark -- 初始化tableView

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[FindCell class] forCellReuseIdentifier:@"FindCell"];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArr.count == 0) {
        return 0;
    }
        
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindCell" forIndexPath:indexPath];
    cell.youHui = _dataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self login];
}
#pragma mark -- 获取数据

- (void)getDataWithURL:(NSString *)URL
{
    [MyRequest getWithURL:URL parameters:nil success:^(NSDictionary *dic) {
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *dic1 in arr) {
            YouHui *youHui = [[YouHui alloc] initWithDictionary:dic1];
            [_dataArr addObject:youHui];
        }
        [_tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark -- 上半部分

- (void)setTopView
{
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 70, 30, 30)];
    headView.image = [UIImage imageNamed:@"btn_user_normal"];
    [self.view addSubview:headView];
    
    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectZero];
    nameLab.font = [UIFont systemFontOfSize:14];
    nameLab.text = @"登录查看积分和金币";
    [self.view addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView.mas_right).offset(20);
        make.top.offset(70);
    }];
    
    UILabel *jiFenLab = [[UILabel alloc] init];
    jiFenLab.text = @"积分:  0";
    jiFenLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:jiFenLab];
    
    [jiFenLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(55);
        make.top.equalTo(nameLab.mas_bottom).offset(20);
    }];
    
    UILabel *jinBiLab = [[UILabel alloc] init];
    jinBiLab.text = @"金币:  0";
    jinBiLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:jinBiLab];
    
    [jinBiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-55);
        make.top.equalTo(nameLab.mas_bottom).offset(20);
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.layer.cornerRadius = 5;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = [UIColor redColor];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(jinBiLab.mas_bottom).offset(20);
        make.height.offset(30);
        make.width.equalTo(self.view.mas_width).multipliedBy(.3);
    }];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.textColor = [UIColor redColor];
    lab.text = @"?如何得到积分";
    lab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.top.equalTo(jinBiLab.mas_bottom).offset(25);
    }];
    UIView *topLine = [[UIView alloc] init];
    topLine.backgroundColor = [UIColor grayColor];
    [self.view addSubview:topLine];
    
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(5);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(Width);
        make.height.offset(1);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLine.mas_bottom).offset(0);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(Width);
        make.height.offset(40);
    }];
    NSArray *arr = @[@"全部",@"优惠券",@"识货礼品卡",@"实物礼品"];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.frame = CGRectMake(Width*i/4, 0, Width/4, 39);
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(reloadTableView:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:button];
        if (i == 0) {
            _moveLine = [[UIView alloc] init];
            _moveLine.frame = CGRectMake(0, 38, Width/4, 2);
            _moveLine.backgroundColor = [UIColor redColor];
            [bgView addSubview:_moveLine];
        }
    }
    
}

- (void)reloadTableView:(UIButton *)button
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _moveLine.frame = CGRectMake(button.frame.origin.x, 38, Width/4, 2);
    [UIView commitAnimations];
    
    [_dataArr removeAllObjects];
    
    if (button.tag == 3) {
        [_tableView reloadData];
        return;
    } else {
        [self getDataWithURL:_urlArr[button.tag]];
    }
}
- (void)login
{
    LoginVC *vc = [[LoginVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -- 创建导航按钮
- (void)setTitleView
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 50, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"F_iocn_normal"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 50, 40);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"D_iocn_normal"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)showMenu
{
    
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
